﻿using System.Collections.Generic;
using System.Text;
using System.Xml;
using UwaySoft.EsheetServer.BizLogic;

namespace UwaySoft.EsheetServer.Help.Template
{
    /// <summary>
    /// 工单模板
    /// </summary>
    public class NewSheetEntity : BaseTemplate
    {
        #region 成员字段

        private string _serialNo;
        private string _state;
        private string _serviceClassId;
        private string _serviceContent;
        private string _acceptNo;
        private string _acceptMgr;
        private string _acceptPhone;
        private string _userName;
        private string _userGrade;
        private string _mainCallNo;
        private string _contactName;
        private string _contactId1;
        private string _contactId2;
        private string _fax;
        private string _email;
        private string _address;
        private string _faultHappenDate;
        private string _lastDate;
        private readonly List<NewSheetItemEntity> _workOrderTarget;

        #endregion

        #region 属 性

        /// <summary>
        /// 工单流水号
        /// </summary>
        public string SerialNo
        {
            get
            {
                CheckString(_serialNo, "缺失工单流水号");
                return _serialNo;
            }
            set
            {
                _serialNo = value;
            }
        }
        /// <summary>
        /// 当前状态
        /// </summary>
        public string State
        {
            get
            {
                CheckString(_state, "缺失当前状态");
                return _state;
            }
            set
            {
                _state = value;
            }
        }
        /// <summary>
        /// 业务类型
        /// </summary>
        public string ServiceClassId
        {
            get
            {
                CheckString(_serviceClassId, "缺失业务类型");
                return _serviceClassId;
            }
            set
            {
                _serviceClassId = value;
            }
        }
        /// <summary>
        /// 业务内容
        /// </summary>
        public string ServiceContent
        {
            get
            {
                CheckString(_serviceContent, "缺失业务内容");
                return FilterHtml(_serviceContent);
            }
            set
            {
                _serviceContent = value;
            }
        }
        /// <summary>
        /// 受理工号
        /// </summary>
        public string AcceptNo
        {
            get
            {
                CheckString(_acceptNo, "缺失受理工号");
                return _acceptNo;
            }
            set
            {
                _acceptNo = value;
            }
        }
        /// <summary>
        /// VIP经理号
        /// </summary>
        public string AcceptMgr
        {
            get
            {
                CheckString(_acceptMgr, "缺失VIP经理号");
                return _acceptMgr;
            }
            set
            {
                _acceptMgr = value;
            }
        }
        /// <summary>
        /// 受理号码
        /// </summary>
        public string AcceptPhone
        {
            get
            {
                CheckString(_acceptPhone, "缺失受理号码");
                return _acceptPhone;
            }
            set
            {
                _acceptPhone = value;
            }
        }
        /// <summary>
        /// 客户姓名
        /// </summary>
        public string UserName
        {
            get
            {
                CheckString(_userName, "缺失客户姓名");
                return _userName;
            }
            set
            {
                _userName = value;
            }
        }
        /// <summary>
        /// 客户级别
        /// </summary>
        public string UserGrade
        {
            get
            {
                CheckString(_userGrade, "缺失客户级别");
                return _userGrade;
            }
            set
            {
                _userGrade = value;
            }
        }
        /// <summary>
        /// 主叫号码
        /// </summary>
        public string MainCallNo
        {
            get
            {
                CheckString(_mainCallNo, "缺失主叫号码");
                return _mainCallNo;
            }
            set
            {
                _mainCallNo = value;
            }
        }
        /// <summary>
        /// 联系人姓名
        /// </summary>
        public string ContactName
        {
            get
            {
                CheckString(_contactName, "缺失联系人姓名");
                return _contactName;
            }
            set
            {
                _contactName = value;
            }
        }
        /// <summary>
        /// 联系电话1
        /// </summary>
        public string ContactId1
        {
            get
            {
                CheckString(_contactId1, "缺失联系电话1");
                return _contactId1;
            }
            set
            {
                _contactId1 = value;
            }
        }
        /// <summary>
        /// 联系电话2
        /// </summary>
        public string ContactId2
        {
            get { return _contactId2; }
            set { _contactId2 = value; }
        }
        /// <summary>
        /// 传真号码
        /// </summary>
        public string Fax
        {
            get { return _fax; }
            set { _fax = value; }
        }
        /// <summary>
        /// 电子邮件
        /// </summary>
        public string Email
        {
            get { return _email; }
            set { _email = value; }
        }
        /// <summary>
        /// 联系地址
        /// </summary>
        public string Address
        {
            get { return _address; }
            set { _address = value; }
        }
        /// <summary>
        /// 受理时间
        /// </summary>
        public string FaultHappenDate
        {
            get { return _faultHappenDate; }
            set { _faultHappenDate = value; }
        }
        /// <summary>
        /// 用户希望反馈时间
        /// </summary>
        public string LastDate
        {
            get { return _lastDate; }
            set { _lastDate = value; }
        }
        /// <summary>
        /// 派单信息
        /// </summary>
        public List<NewSheetItemEntity> WorkOrderTarget
        {
            get { return _workOrderTarget; }
        }

        #endregion

        /// <summary>
        /// 构造函数
        /// </summary>
        public NewSheetEntity()
        {
            _workOrderTarget = new List<NewSheetItemEntity>();
        }
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="xmlDocument">XML模板</param>
        public NewSheetEntity(XmlDocument xmlDocument) : this()
        {
            SerialNo = GetNodeInnerText(xmlDocument, "/Accept/serialNo");
            State = GetNodeInnerText(xmlDocument, "/Accept/state");
            ServiceClassId = GetNodeInnerText(xmlDocument, "/Accept/serviceClassId");
            ServiceContent = GetNodeInnerText(xmlDocument, "/Accept/serviceContent");
            AcceptNo = GetNodeInnerText(xmlDocument, "/Accept/acceptNo");
            AcceptMgr = GetNodeInnerText(xmlDocument, "/Accept/acceptMgr");
            AcceptPhone = GetNodeInnerText(xmlDocument, "/Accept/acceptPhone");
            UserName = GetNodeInnerText(xmlDocument, "/Accept/userName");
            UserGrade = GetNodeInnerText(xmlDocument, "/Accept/userGrade");
            MainCallNo = GetNodeInnerText(xmlDocument, "/Accept/mainCallNo");
            ContactName = GetNodeInnerText(xmlDocument, "/Accept/contactName");
            ContactId1 = GetNodeInnerText(xmlDocument, "/Accept/contactId1");
            ContactId2 = GetNodeInnerText(xmlDocument, "/Accept/contactId2");
            Fax = GetNodeInnerText(xmlDocument, "/Accept/fax");
            Email = GetNodeInnerText(xmlDocument, "/Accept/email");
            Address = GetNodeInnerText(xmlDocument, "/Accept/address");
            FaultHappenDate = GetNodeInnerText(xmlDocument, "/Accept/faultHappenDate");
            LastDate = GetNodeInnerText(xmlDocument, "/Accept/lastDate");

            // 派单信息
            var nodeList = GetNodes(xmlDocument, "/Accept/targetInfo");
            if (nodeList.Count > 0)
            {
                foreach (XmlNode node in nodeList)
                {
                    WorkOrderTarget.Add(new NewSheetItemEntity
                                         {
                                             AcceptGroup = GetNodeInnerText(node, "acceptGroup"),
                                             AcceptNode = GetNodeInnerText(node, "acceptNode"),
                                             Acceptor = GetNodeInnerText(node, "acceptor"),
                                             AcceptSuggest = GetNodeInnerText(node, "acceptSuggest"),
                                             AcceptTime = GetNodeInnerText(node, "acceptTime"),
                                             TargetSpace = GetNodeInnerText(node, "targetSpace")
                                         });
                }
            }
        }

        /// <summary>
        /// 重写ToString
        /// </summary>
        /// <returns>返回XML格式字符串</returns>
        public override string ToString()
        {
            var bldr = new StringBuilder();
            bldr.Append("<?xml version=\"1.0\" encoding=\"gb2312\"?>");
            bldr.Append("<Accept>");
            bldr.AppendFormat("<serialNo chinaName=\"流水号\">{0}</serialNo>", SerialNo);
            bldr.AppendFormat("<state chinaName=\"当前状态\">{0}</state>", State);
            bldr.AppendFormat("<serviceClassId chinaName=\"业务类型\">{0}</serviceClassId>", ServiceClassId);
            bldr.AppendFormat("<serviceContent chinaName=\"业务内容\">{0}</serviceContent>", ServiceContent);
            bldr.AppendFormat("<acceptNo chinaName=\"受理工号\">{0}</acceptNo>", AcceptNo);
            bldr.AppendFormat("<acceptMgr chinaName=\"VIP经理号\">{0}</acceptMgr>", AcceptMgr);
            bldr.AppendFormat("<acceptPhone chinaName=\"受理号码\">{0}</acceptPhone>", AcceptPhone);
            bldr.AppendFormat("<userName chinaName=\"客户姓名\">{0}</userName>", UserName);
            bldr.AppendFormat("<userGrade chinaName=\"客户级别\">{0}</userGrade>", UserGrade);
            bldr.AppendFormat("<mainCallNo chinaName=\"主叫号码\">{0}</mainCallNo>", MainCallNo);
            bldr.AppendFormat("<contactName chinaName=\"联系人姓名\">{0}</contactName>", ContactName);
            bldr.AppendFormat("<contactId1 chinaName=\"联系电话1\">{0}</contactId1>", ContactId1);
            bldr.AppendFormat("<contactId2 chinaName=\"联系电话2\">{0}</contactId2>", ContactId2);
            bldr.AppendFormat("<fax chinaName=\"传真号码\">{0}</fax>", Fax);
            bldr.AppendFormat("<email chinaName=\"电子邮件\">{0}</email>", Email);
            bldr.AppendFormat("<address chinaName=\"联系地址\">{0}</address>", Address);
            bldr.AppendFormat("<faultHappenDate chinaName=\"受理时间\">{0}</faultHappenDate>", FaultHappenDate);
            bldr.AppendFormat("<lastDate chinaName=\"用户希望反馈时间\">{0}</lastDate>", LastDate);
            // 派单
            if (WorkOrderTarget != null && WorkOrderTarget.Count > 0)
            {
                foreach (var targetInfo in WorkOrderTarget)
                {
                    bldr.Append("<targetInfo chinaName=\"派单\">");
                    bldr.Append(targetInfo.ToString());
                    bldr.Append("</targetInfo>");
                }
            }
            bldr.Append("</Accept>");
            return bldr.ToString();
        }
    }
}
